package com.easonji.attendance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.easonji.admin.manager.UserManager;
import com.easonji.attendance.mapper.VacationMapper;
import com.easonji.attendance.model.Vacation;
import com.easonji.attendance.query.VacationQuery;
import com.easonji.attendance.service.IVacationService;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.factory.PageFactory;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.util.StringUtils;
import com.easonji.core.shiro.ShiroUser;
import com.easonji.core.shiro.util.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 假期 服务实现类
 * </p>
 *
 * @author easonji
 * @since 2018-09-23
 */
@Service
public class VacationServiceImpl extends AbstractBaseService<Vacation> implements IVacationService {

    @Resource
    private VacationMapper vacationMapper;

    @Resource
    private UserManager userManager;

    @Override
    protected JSONObject warp2Json(Vacation vacation) {
       JSONObject result = JsonUtils.parseObject(vacation);
        result.put("vUserName",userManager.getUserNameById(vacation.getvUser()));
       return result;
    }


    @Override
    public Object queryList(VacationQuery vacationQuery){

        // 分页信息
        Page<Vacation> page = new PageFactory<Vacation>().defaultPage();
        // 查询条件
        Wrapper<Vacation> wrapper = new EntityWrapper<>();

        Integer vUser = vacationQuery.getvUser();
        if (StringUtils.isNotEmpty(vUser)) {
            wrapper.eq("v_user",vUser);
        }
        String vYear = vacationQuery.getvYear();
        if (StringUtils.isNotEmpty(vYear)) {
            wrapper.like("v_year", vYear);
        }

        List<Vacation> vacations = vacationMapper.selectPage(page, wrapper);

        return new PageInfo(warp(vacations), page.getTotal());
    }

    @Override
    public Object queryRowById(Integer vId) {
        Vacation vacation = vacationMapper.selectById(vId);
        return warp(vacation);
    }

    @Override
    public Integer save(Vacation vacation) {
        ShiroUser shiroUser = ShiroUtils.getUser();
        //设置剩余天数
        vacation.setvDayNumRemain(vacation.getvDayNum());
        vacation.setvCreateUser(shiroUser.getUserId());
        vacation.setvUpdateDate(new Date());
        vacation.setvUpdateUser(shiroUser.getUserId());
        vacation.setvUpdateDate(new Date());
        return vacationMapper.insert(vacation);
    }

    @Override
    public Integer update(Vacation vacationForm) {
        ShiroUser shiroUser = ShiroUtils.getUser();
        vacationForm.setvUpdateUser(shiroUser.getUserId());
        vacationForm.setvUpdateDate(new Date());
        Vacation vacation = new Vacation();
        BeanUtils.copyProperties(vacationForm,vacation);
        return vacationMapper.updateById(vacation);
    }

    @Override
    public Integer delete(Integer vId) {
        Wrapper<Vacation> wrapper = new EntityWrapper<>();
        wrapper.eq("v_id", vId);
        return vacationMapper.delete(wrapper);
    }

    @Override
    public Integer queryVidByUserAndYear(Integer vUser,String vYear) {
        Integer result = null;
        // 查询条件
        Wrapper<Vacation> wrapper = new EntityWrapper<>();
        wrapper.eq("v_user",vUser);
        wrapper.eq("v_year", vYear);
        vacationMapper.selectList(wrapper);
        List<Vacation> vacations = vacationMapper.selectList(wrapper);
        if (StringUtils.isNotEmpty(vacations)) {
            result = vacations.get(0).getvId();
        }
        return result;
    }

}
